package com.lej.dataservice.club.http

import android.net.ParseException
import com.google.gson.JsonParseException
import com.google.gson.stream.MalformedJsonException
import org.apache.http.conn.ConnectTimeoutException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.net.ssl.SSLException


/**
 * 网络层异常处理
 */
object ExceptionHandle {
    private const val UNAUTHORIZED = 401
    private const val FORBIDDEN = 403
    private const val NOT_FOUND = 404
    private const val REQUEST_TIMEOUT = 408
    private const val INTERNAL_SERVER_ERROR = 500
    private const val SERVICE_UNAVAILABLE = 503
    fun handleException(e: Throwable?): ResponseThrowable {
        return when (e) {
            is HttpException -> {
                ResponseThrowable(e, HTTP_ERROR).apply {
                    message = when (e.code()) {
                        UNAUTHORIZED -> "操作未授权"
                        FORBIDDEN -> "请求被拒绝"
                        NOT_FOUND -> "资源不存在"
                        REQUEST_TIMEOUT -> "服务器执行超时"
                        INTERNAL_SERVER_ERROR -> "服务器内部错误"
                        SERVICE_UNAVAILABLE -> "服务器不可用"
                        else -> e.message() ?: "网络错误"
                    }
                }
            }
            is JsonParseException, is JSONException, is ParseException, is MalformedJsonException -> {
                ResponseThrowable(e, PARSE_ERROR).apply {
                    message = "解析错误:" + e.message;stackTrace = e.stackTrace
                }
            }
            is ConnectException -> {
                ResponseThrowable(e, NET_WORD_ERROR).apply { message = "连接失败" }
            }
            is SSLException -> {
                ResponseThrowable(e, SSL_ERROR).apply { message = "证书验证失败" }
            }
            is ConnectTimeoutException -> {
                ResponseThrowable(e, TIMEOUT_ERROR).apply { message = "连接超时" }
            }
            is SocketTimeoutException -> {
                ResponseThrowable(e, TIMEOUT_ERROR).apply { message = "连接超时" }
            }
            is UnknownHostException -> {
                ResponseThrowable(e, TIMEOUT_ERROR).apply { message = "主机地址未知" }
            }
            else -> {
                ResponseThrowable(e, UNKNOWN).apply { message = e?.message ?: "未知错误" }
            }
        }
    }
}